Thread: [C] Drawing A National Flag With Stars (While Loops And If Statements)

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    2

    [C] Drawing A National Flag With Stars (While Loops And If Statements)

    Hey guys!
    I am new to the forum and want to say hi to everybody!

    Currently I am working on an exercise to draw a national flag by using while and if statements. I am learning the programming language in own efforts, so I am not attending any school or what so ever.

    Alright to point out my problem:
    I wrote a function to draw the flag I desired (Danish flag). Unfortunately it only draws the very first row and I don't understand why it is only drawing the first row. I would be really grateful to get some hints what I actually did with the code to behave that way. This is the code:

    Code:
    
    
    Code:
    /* Create a Danish Flag with the use of loops and functions
     */
    
    
    /* 
     * File:   main.c
     * Author: Epi
     *
     * Created on 20. Mai 2016, 18:29
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define WIDTH 11
    #define HEIGHT 6
    
    
    void drawFlag (int width, int height);
    
    
    int main(int argc, char** argv) {
    
    
        int width = WIDTH;
        int height = HEIGHT;
        
        drawFlag (width, height);
        
        return (EXIT_SUCCESS);
    }
    
    
    void drawFlag (int width, int height){
        int counter = 0;
        int drawHeight = 0;
        int drawWidth = 0;
        while (drawHeight < height){
            if (drawHeight != 3 || drawWidth != 4){
                while (counter < width){
                    if (counter == 3){
                        printf (" ");
                    }else{
                        printf ("*");
                    }
                    counter++;
                }
            }else{
                while (counter < width){
                    printf (" ");
                    counter++;
                }
            }
            printf ("\n");
            drawHeight++;
            drawWidth++;
        }
    }
    Edit: sry i cant get rid of the first code window, it reappears all the time. Cheers!
    Last edited by C_Newbe; 05-21-2016 at 05:21 AM. Reason: Double Code Window

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You need to reset counter to 0 for each new row.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    2
    Quote Originally Posted by Salem View Post
    You need to reset counter to 0 for each new row.
    Oh yeah, that was the problem, thx man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-20-2012, 07:37 PM
  2. While statements and loops
    By Native in forum C Programming
    Replies: 18
    Last Post: 10-27-2010, 03:11 PM
  3. Flag-Controlled Loops
    By CPPN0OB in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2010, 10:35 AM
  4. For Loops (Making Stars)
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2007, 11:34 PM
  5. Black and white stars or Color stars
    By Silvercord in forum Game Programming
    Replies: 10
    Last Post: 04-10-2003, 01:40 PM

Tags for this Thread